package com.example.myviewgroup;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;

/**
 * author: ts
 * created on: 2021/8/28 下午2:52
 * description:
 */
public class FlexBoxLayoutViewGroup extends MyFlexBoxLayout implements MyBaseAdapter.OnDataChangedListener {

    public MyBaseAdapter mMyBaseAdapter;

    public FlexBoxLayoutViewGroup(Context context) {
        super(context);
    }

    public FlexBoxLayoutViewGroup(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void setMyAdapter(MyBaseAdapter myBaseAdapter) {
        if (myBaseAdapter == null) {
            throw new NullPointerException("adapter is null");
        }
        mMyBaseAdapter = myBaseAdapter;
        mMyBaseAdapter.setOnDataChangedListener(this);
        updateView();
    }

    private void updateView() {
        removeAllViews();
        int childViewCounts = mMyBaseAdapter.getItemCount();
        for (int i = childViewCounts - 1; i >= 0; i--) {
            View view = mMyBaseAdapter.getView(i, this);
            addView(view);
        }
    }

    @Override
    public void onChanged() {
        updateView();
    }
}
